package utils;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/28 16:06
 * @description:
 */
public class ServerResponse<T> {
    /**
     * response code
     */
    private Integer status;
    /**
     * response message
     */
    private String msg;
    /**
     * response object
     */
    private T data;

    /**
     * create a server response object only with response code
     *
     * @param status ServerResponseCode.SUCCESS, ServerResponseCode.ERROR
     */
    private ServerResponse(Integer status) {
        this.status = status;
    }

    /**
     * create a server response object with response code and response message
     *
     * @param status ServerResponseCode.SUCCESS, ServerResponseCode.ERROR
     * @param msg    response message
     */
    private ServerResponse(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    /**
     * create a server response object with response code and response object
     *
     * @param status ServerResponseCode.SUCCESS, ServerResponseCode.ERROR
     * @param data   response object
     */
    private ServerResponse(Integer status, T data) {
        this.status = status;
        this.data = data;
    }

    /**
     * create a server response object with response code, response message and response object
     *
     * @param status ServerResponseCode.SUCCESS, ServerResponseCode.ERROR
     * @param msg    response message
     * @param data   response object
     */
    private ServerResponse(Integer status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    /**
     * get response code
     *
     * @return ServerResponseCode.SUCCESS, ServerResponseCode.ERROR
     */
    public Integer getStatus() {
        return status;
    }

    /**
     * get response message
     *
     * @return response message
     */
    public String getMsg() {
        return msg;
    }

    /**
     * get response data
     *
     * @return response data
     */
    public T getData() {
        return data;
    }

    /**
     * current response is success
     *
     * @return success return true
     */
    public boolean success() {
        return this.status.equals(ServerResponseCode.SUCCESS);
    }

    /**
     * create a server success response with message
     *
     * @param msg response message as user defined
     * @param <T> object type
     * @return server response object
     */
    public static <T> ServerResponse<T> createSuccess(String msg) {
        return new ServerResponse<>(ServerResponseCode.SUCCESS, msg);
    }

    public static <T> ServerResponse<T> createSuccess(String msg, T date) {
        return new ServerResponse<>(ServerResponseCode.SUCCESS, date);
    }


    /**
     * create a server success response with message
     *
     * @param object response object as user defined
     * @param <T>    object type
     * @return server response object
     */
    public static <T> ServerResponse<T> createSuccess(T object) {
        return new ServerResponse<>(ServerResponseCode.SUCCESS, object);
    }

    /**
     * create a server success response with message and object
     *
     * @param msg response message as user defined
     * @param object response object as user defined
     * @param <T> object type
     * @return server response object
     */
//  public static <T> ServerResponse<T> createSuccess(String msg, T object) {
//    return new ServerResponse<>(ServerResponseCode.SUCCESS, msg, object);
//  }

    /**
     * create a server error response with message
     *
     * @param msg response message as user defined
     * @param <T> object type
     * @return server response object
     */
    public static <T> ServerResponse<T> createError(String msg) {
        return new ServerResponse<>(ServerResponseCode.ERROR, msg);
    }

    /**
     * create a server error response with message
     *
     * @param object response object as user defined
     * @param <T>    object type
     * @return server response object
     */
    public static <T> ServerResponse<T> createError(T object) {
        return new ServerResponse<>(ServerResponseCode.ERROR, object);
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * create a server error response with message and object
     *
     * @param msg    response message as user defined
     * @param object response object as user defined
     * @param <T>    object type
     * @return server response object
     */
    public static <T> ServerResponse<T> createError(String msg, T object) {
        return new ServerResponse<>(ServerResponseCode.ERROR, msg, object);
    }

    public static <T> ServerResponse<T> createError(int i, String msg) {
        return new ServerResponse<>(i, msg);
    }
}
